package com.jeeplus.modules.monitor.utils;    /**
 * @title: SmbFileUtil
 * @projectName jeeplus
 * @description: TODO
 * @author Administrator
 * @date 2019/8/615:23
 */

import jcifs.smb.*;

import javax.swing.text.StyledEditorKit;
import java.io.*;
import java.net.MalformedURLException;
import java.sql.Ref;
import java.util.ArrayList;
import java.util.List;

import com.jeeplus.modules.monitor.entity.*;

public class SmbFileUtil {
    /**
     * 共享文件夹配置
     */
    private static String HOST = "10.0.17.127";
    private static String USER_NAME = "administrator";
    private static String PASSWORD = "bjhl";
    private static String PATH = "/shareFile/";
    private static String REMOTE_URL = "smb://" + USER_NAME + ":" + PASSWORD + "@" + HOST + PATH;

    /**
     * 描述
     *
     * @param ipaddress  服务器地址
     * @param userName   用户名
     * @param passWd     密码
     * @param remotePath 远程目录
     * @return
     * @Author LH
     * @Description //TODO
     * @Date 13:32 2019/5/6
     **/
    public SmbFileUtil(String ipaddress, String userName, String passWd, String remotePath) {

        HOST = ipaddress;
        USER_NAME = userName;
        PASSWORD = passWd;
        PATH = remotePath;
    }

    /**
     * 下载文件
     *
     * @param fileName 文件名 + .文件类型 1.txt
     * @param localDir 文件保存文件夹
     * @throws IOException
     */
    public Result smbGet(String fileName, String localDir, boolean isRename) throws IOException {
        Result resultInfo = new Result();
        InputStream in = null;
        OutputStream out = null;
        StringBuffer result = new StringBuffer();
        try {
            SmbFile remoteFile = new SmbFile(REMOTE_URL + fileName);
            if (remoteFile == null || !remoteFile.exists()) {
                //文件不存在
                resultInfo.returnInfo = ReturnInfo.Error;
                resultInfo.errorInfo = "文件不存在";
                return resultInfo;
            }
            if (new File(localDir + fileName).exists()) {
                resultInfo.returnInfo = ReturnInfo.Error;
                resultInfo.errorInfo = "本地文件己存在";
                return resultInfo;
            }
            result.append(localDir).append(File.separator).append(fileName);
            File localFile = new File(result.toString());
            in = new BufferedInputStream(new SmbFileInputStream(remoteFile));
            out = new BufferedOutputStream(new FileOutputStream(localFile));
            byte[] buffer = new byte[1024];
            int bytesToRead = -1;
            while ((bytesToRead = in.read(buffer)) != -1) {
                out.write(buffer, 0, bytesToRead);
            }
            out.flush();
            //判断本地是否下载文件
            if (new File(localDir + fileName).exists()) {
                resultInfo.returnInfo = ReturnInfo.Success;
            }
            //是否修改服务器文件
            if (isRename) {
                if (resultInfo.returnInfo == ReturnInfo.Success)
                    remoteFile.renameTo(new SmbFile(REMOTE_URL + fileName + ".TORCV"));
            }
            return resultInfo;
        } catch (Exception e) {
            e.printStackTrace();
            resultInfo.returnInfo = ReturnInfo.Exception;
            resultInfo.errorInfo = e.getMessage();
            return resultInfo;
        } finally {
            if (in != null) {
                in.close();
            }
            if (out != null) {
                out.close();
            }
        }
    }

    /**
     * 上传文件
     *
     * @param localFilePath 文件路径 + 文件名 + .文件类型
     * @throws IOException
     */
    public void smbPut(String localFilePath, String remotePath) throws IOException {
        InputStream in = null;
        OutputStream out = null;
        try {
            File localFile = new File(localFilePath);
            String fileName = localFile.getName();
            SmbFile remoteFile = new SmbFile(REMOTE_URL + remotePath + fileName);
            in = new BufferedInputStream(new FileInputStream(localFile));
            out = new BufferedOutputStream(new SmbFileOutputStream(remoteFile));
            byte[] buffer = new byte[1024];
            int bytesToRead = -1;
            while ((bytesToRead = in.read(buffer)) != -1) {
                out.write(buffer, 0, bytesToRead);
            }
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                in.close();
            }
            if (out != null) {
                out.close();
            }
        }
    }

    /**
     * 远程文件删除 文件名 + .文件类型
     *
     * @param fileName
     */
    public void smbDelete(String fileName) {
        SmbFile file = null;
        try {
            file = new SmbFile(REMOTE_URL + fileName);
            if (file.exists()) {
                file.delete();
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (SmbException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取文件列表
     *
     * @return java.util.List<java.lang.String>
     * @Author LH
     * @Description //TODO
     * @Date 12:00 2019/5/6
     * @Param []
     **/
    public List<String> listFile() {
        List<String> fileList = new ArrayList<String>();
        try {
            SmbFile[] smbFiles = new SmbFile(REMOTE_URL).listFiles(new SmbFileFilter() {
                @Override
                public boolean accept(SmbFile smbFile) throws SmbException {
                    if (smbFile.getName().endsWith(".TORCV")) {
                        return true;
                    }
                    return false;
                }
            });
            for (SmbFile file : smbFiles) {
                fileList.add(file.getName());
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return fileList;
    }

    public static void main(String[] args) throws IOException {

        SmbFileUtil fileUtil = new SmbFileUtil("10.0.17.127", "administrator", "bjhl", "/shareFile/");
//        fileUtil.listFile();
//        fileUtil.smbGet("a.log.TORCV","d:/",false);
        for (String str : fileUtil.listFile()) {
            System.out.println(str);
        }
    }
}
